﻿#include "TreeViewPropertyEditEntry.h"
#include <QTreeView>

#include "IntegerEditEntry.h"
#include "BooleanEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(TreeView)

struct TreeViewPropertyEditEntry::TreeViewPropertyEditEntryPrivate {
    QPointer<QTreeView> view;

    QPointer<IntegerEditEntry> entryAutoExpandDelay;
    QPointer<IntegerEditEntry> entryIndentation;
    QPointer<BooleanEditEntry> entryRootIsDecorated;
    QPointer<BooleanEditEntry> entryUniformRowHeights;
    QPointer<BooleanEditEntry> entryItemsExpandable;
    QPointer<BooleanEditEntry> entrySortingEnabled;
    QPointer<BooleanEditEntry> entryAnimated;
    QPointer<BooleanEditEntry> entryAllColumnsShowFocus;
    QPointer<BooleanEditEntry> entryWordWrap;
    QPointer<BooleanEditEntry> entryHeaderHidden;
    QPointer<BooleanEditEntry> entryExpandsOnDoubleClick;
};

TreeViewPropertyEditEntry::~TreeViewPropertyEditEntry() { }

TreeViewPropertyEditEntry::TreeViewPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TreeViewPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

TreeViewPropertyEditEntry::TreeViewPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TreeViewPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void TreeViewPropertyEditEntry::setObject(void *object) {
    p->view = static_cast<QTreeView*>(object);
}

void TreeViewPropertyEditEntry::onTick() {
    if(!p->view || !isVisible()) {
        return ;
    }

    p->entryAutoExpandDelay->setValue(p->view->autoExpandDelay());
    p->entryIndentation->setValue(p->view->indentation());
    p->entryRootIsDecorated->setValue(p->view->rootIsDecorated());
    p->entryUniformRowHeights->setValue(p->view->uniformRowHeights());
    p->entryItemsExpandable->setValue(p->view->itemsExpandable());
    p->entrySortingEnabled->setValue(p->view->isSortingEnabled());
    p->entryAnimated->setValue(p->view->isAnimated());
    p->entryAllColumnsShowFocus->setValue(p->view->allColumnsShowFocus());
    p->entryWordWrap->setValue(p->view->wordWrap());
    p->entryHeaderHidden->setValue(p->view->isHeaderHidden());
    p->entryExpandsOnDoubleClick->setValue(p->view->expandsOnDoubleClick());
}

void TreeViewPropertyEditEntry::initUi() {
    p->entryAutoExpandDelay = new IntegerEditEntry;
    appendChildEntry(p->entryAutoExpandDelay);

    p->entryIndentation = new IntegerEditEntry;
    appendChildEntry(p->entryIndentation);

    p->entryRootIsDecorated = new BooleanEditEntry;
    appendChildEntry(p->entryRootIsDecorated);

    p->entryUniformRowHeights = new BooleanEditEntry;
    appendChildEntry(p->entryUniformRowHeights);

    p->entryItemsExpandable = new BooleanEditEntry;
    appendChildEntry(p->entryItemsExpandable);

    p->entrySortingEnabled = new BooleanEditEntry;
    appendChildEntry(p->entrySortingEnabled);

    p->entryAnimated = new BooleanEditEntry;
    appendChildEntry(p->entryAnimated);

    p->entryAllColumnsShowFocus = new BooleanEditEntry;
    appendChildEntry(p->entryAllColumnsShowFocus);

    p->entryWordWrap = new BooleanEditEntry;
    appendChildEntry(p->entryWordWrap);

    p->entryHeaderHidden = new BooleanEditEntry;
    appendChildEntry(p->entryHeaderHidden);

    p->entryExpandsOnDoubleClick = new BooleanEditEntry;
    appendChildEntry(p->entryExpandsOnDoubleClick);
}

void TreeViewPropertyEditEntry::retranslateUi() {
    setTitle(tr("QTreeView"));
    p->entryAutoExpandDelay->setTitle(tr("autoExpandDelay"));
    p->entryIndentation->setTitle(tr("indentation"));
    p->entryRootIsDecorated->setTitle(tr("rootIsDecorated"));
    p->entryUniformRowHeights->setTitle(tr("uniformRowHeights"));
    p->entryItemsExpandable->setTitle(tr("itemsExpandable"));
    p->entrySortingEnabled->setTitle(tr("sortingEnabled"));
    p->entryAnimated->setTitle(tr("animated"));
    p->entryAllColumnsShowFocus->setTitle(tr("allColumnsShowFocus"));
    p->entryWordWrap->setTitle(tr("wordWrap"));
    p->entryHeaderHidden->setTitle(tr("headerHidden"));
    p->entryExpandsOnDoubleClick->setTitle(tr("expandsOnDoubleClick"));
}
